

//
//  LoginMainView.m
//  FinancialStore
//
//  Created by 王晓萌 on 16/8/8.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//

#import "LoginMainView.h"
#import "MeConfigUI.h"
#import "LoginNormalCell.h"
#import "UIImage+Stretch.h"

@interface LoginMainView () 
@property (nonatomic,strong) UIImageView *iconImg;

@property (nonatomic,strong) UIImageView *backgroudImg;

//footerView & sub
@property (nonatomic,strong) UIView *footerView;

@property (nonatomic,strong) UIButton *protocolBtn;
@property (nonatomic,strong) UIButton *rightProtocolBtn;
@property (nonatomic,strong) UIButton *forgetPwdBtn;

//moreView & sub
@property (nonatomic,strong) UIView *moreView;
@property (nonatomic,strong) UIButton *registBtn;
@property (nonatomic,strong) UILabel *lineLbl;
@property (nonatomic,strong) UIView *leftSep;
@property (nonatomic,strong) UIView *rightSep;
@property (nonatomic,strong) UIButton *quickLogin;

@end


@implementation LoginMainView

#pragma  mark - init
- (instancetype)init{
    if (self = [super init]) {
        
        [self setFrame:[[UIScreen mainScreen] bounds]];
        self.backgroundColor = [UIColor colorWithString:UI_COLOR_MAIN_3];
        [self setUpSubViews];
        [self updateConstraintsIfNeeded];
    }
    return self;
}


#pragma  mark - sub init
- (void)setUpSubViews{
    
    [self setUpNormal];
    [self setUpMore];
    
}


- (void)setUpNormal{
    self.backgroudImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:UI_IMAGE_BACKGROUD_ICON]];
    [self addSubview:self.backgroudImg];
    self.iconImg = [[UIImageView alloc] init];
  self.iconImg.hidden = YES;
    [self addSubview:self.iconImg];
    self.iconImg.image = [UIImage imageNamed:UI_IMAGE_LOGIN_LOGO];
    self.coreView = [[UITableView alloc] initWithFrame:CGRectNull style:UITableViewStylePlain];
    [self addSubview:self.coreView];
    self.coreView.backgroundColor = [UIColor clearColor];
    self.coreView.separatorStyle = UITableViewCellStyleDefault;
    self.coreView.scrollEnabled = NO;
    
    self.footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, UI_LOGIN_FOOTVIEW_HEIGHT)];
    self.footerView.backgroundColor = [UIColor clearColor];
    self.coreView.tableFooterView = self.footerView;
    
    self.mainEventBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.footerView addSubview:self.mainEventBtn];
    
    self.mainEventBtn.layer.cornerRadius = ME_RADIUS;
    self.mainEventBtn.layer.masksToBounds = YES;
    [self.mainEventBtn setTitle:@"登录" forState:UIControlStateNormal];
    [self.mainEventBtn.titleLabel setFont:[UIFont systemFontOfSize:17]];
    [self initLoginBtn];
    [self.mainEventBtn setBackgroundImage:[UIImage getImageFromColor:[UIColor colorWithString:@"3695ff" andAlp:0.4]] forState:UIControlStateNormal];
    
    
    
    self.protocolBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  self.protocolBtn.hidden = YES;
    [self.footerView addSubview:self.protocolBtn];
    [self.protocolBtn.titleLabel setFont:[UIFont systemFontOfSize:LOGIN_FONT_SIZE_SUB_1]];
    [self.protocolBtn setImage:[UIImage imageNamed:UI_IMAGE_LOGIN_NONE] forState:UIControlStateNormal];
    [self.protocolBtn setImage:[UIImage imageNamed:UI_IMAGE_LOGIN_RIGIT] forState:UIControlStateSelected];
    [self.protocolBtn addTarget:self action:@selector(protocolClick:) forControlEvents:UIControlEventTouchUpInside];
     self.protocolBtn.selected = YES;
    
    self.rightProtocolBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  self.rightProtocolBtn.hidden = YES;
    [self.footerView addSubview:self.rightProtocolBtn];
    [self.rightProtocolBtn.titleLabel setFont:[UIFont systemFontOfSize:LOGIN_FONT_SIZE_SUB_1]];
    [self.rightProtocolBtn setTitleColor:[[UIColor colorWithString:LOGIN_COLOR_BG_1] colorWithAlphaComponent:LOGIN_COLOR_ALPHA_1] forState:UIControlStateNormal];
    [self.rightProtocolBtn setTitle:@"同意《用户协议》" forState:UIControlStateNormal];
    [self.rightProtocolBtn addTarget:self action:@selector(rightProtocolClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.forgetPwdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.footerView addSubview:self.forgetPwdBtn];
    [self.forgetPwdBtn.titleLabel setFont:[UIFont systemFontOfSize:LOGIN_FONT_SIZE_SUB_1]];
    [self.forgetPwdBtn setTitleColor:[[UIColor colorWithString:LOGIN_COLOR_BG_1] colorWithAlphaComponent:LOGIN_COLOR_ALPHA_1] forState:UIControlStateNormal];
    [self.forgetPwdBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [self.forgetPwdBtn addTarget:self action:@selector(forgetPwd:) forControlEvents:UIControlEventTouchUpInside];
    self.forgetPwdBtn.tag = 1;

}

- (void)setUpMore{
    self.moreView = [UIView new];
    [self addSubview:self.moreView];
    self.moreView.backgroundColor = [UIColor clearColor];
    
    self.registBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.moreView addSubview:self.registBtn];
    self.registBtn.tag = 2;
    NSString *str = [NSString stringWithFormat:@"还没有账号？立即注册 >"];
    NSUInteger length = [str length];
    NSDictionary *attrDic = @{
                              NSFontAttributeName : [UIFont systemFontOfSize:14],
                              NSForegroundColorAttributeName :[[UIColor colorWithString:LOGIN_COLOR_BG_1] colorWithAlphaComponent:LOGIN_COLOR_ALPHA_1]
                              };
    NSMutableAttributedString *strM = [[NSMutableAttributedString alloc] initWithString:str];
    [strM addAttributes:attrDic range:NSMakeRange(0, length)];
    [strM addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(6, length - 8)];
    [self.registBtn setAttributedTitle:strM forState:UIControlStateNormal];
    [self.registBtn addTarget:self action:@selector(registUser:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.lineLbl = [[UILabel alloc] init];
    [self.moreView addSubview:self.lineLbl];
    self.lineLbl.font = [UIFont systemFontOfSize:11];
    self.lineLbl.textColor = [[UIColor colorWithString:LOGIN_COLOR_BG_1] colorWithAlphaComponent:LOGIN_COLOR_ALPHA_DEEP_1];
    self.lineLbl.text = @"或";
    
    self.leftSep = [UIView new];
    [self.moreView addSubview:self.leftSep];
    self.leftSep.backgroundColor = [[UIColor colorWithString:LOGIN_COLOR_BG_1] colorWithAlphaComponent:LOGIN_COLOR_ALPHA_DEEP_1];

    self.rightSep = [UIView new];
    [self.moreView addSubview:self.rightSep];
    self.rightSep.backgroundColor = [[UIColor colorWithString:LOGIN_COLOR_BG_1] colorWithAlphaComponent:LOGIN_COLOR_ALPHA_DEEP_1];

    
    self.quickLogin = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.moreView addSubview:self.quickLogin];
    self.quickLogin.layer.cornerRadius = ME_RADIUS;
    self.quickLogin.layer.masksToBounds = YES;
    [self.quickLogin setBackgroundImage:[UIImage getImageFromColor:[UIColor colorWithString:LOGIN_COLOR_BG_1 andAlp:0.3]] forState:UIControlStateNormal];
    [self.quickLogin setBackgroundImage:[UIImage getImageFromColor:[UIColor colorWithString:LOGIN_COLOR_BG_1 andAlp:0.8]] forState:UIControlStateSelected];
    [self.quickLogin.titleLabel setFont:[UIFont systemFontOfSize:17]];
    [self.quickLogin setTitleColor:[UIColor colorWithString:LOGIN_COLOR_BG_1] forState:UIControlStateNormal];
    [self.quickLogin setTitle:@"快速登录" forState:UIControlStateNormal];
    [self.quickLogin addTarget:self action:@selector(quickLogin:) forControlEvents:UIControlEventTouchUpInside];
    
    self.quickLogin.tag = 0;
}


#pragma mark - update constraints
- (void)updateConstraints{
    [super updateConstraints];
    [self makeConstraints];
}


#pragma  mark -sub update
- (void)makeConstraints{
    [self makeNormalContraints];
    [self makeMoreContraints];
}

- (void)makeNormalContraints{
    [self.backgroudImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.right.bottom.equalTo(self);
    }];

    
    [self.iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(UI_LOGIN_ICONIMG_TOP);
        make.centerX.equalTo(self);
        make.width.mas_equalTo(UI_LOGIN_ICONIMG_WIDTH);
        make.height.mas_equalTo(UI_LOGIN_ICONIMG_HEIGHT);
    }];
    
    [self.coreView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(UI_LOGIN_COREVIEW_TOP);
        make.left.equalTo(self).offset(UI_LOGIN_COREVIEW_LEFT);
        make.right.equalTo(self).offset(-UI_LOGIN_COREVIEW_LEFT);
        make.height.mas_equalTo(UI_LOGIN_COREVIEW_HEIGHT);
    }];
    
    [self.mainEventBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.equalTo(self.footerView);
        make.height.mas_equalTo(UI_LOGIN_MAINEVENTBTN_HEIGHT);
    }];
    
    
    [self.protocolBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mainEventBtn).offset(0);
        make.top.equalTo(self.mainEventBtn.mas_bottom).with.offset(UI_LOGIN_PROTOCOLBTN_TOP);
        make.height.mas_equalTo(UI_LOGIN_PROTOCOLBTN_HEIGHT);
    }];
    
    [self.rightProtocolBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.protocolBtn.mas_right).offset(RealValueW(5));
        make.top.equalTo(self.mainEventBtn.mas_bottom).with.offset(UI_LOGIN_PROTOCOLBTN_TOP);
        make.height.mas_equalTo(UI_LOGIN_PROTOCOLBTN_HEIGHT);
    }];
    
    [self.forgetPwdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.protocolBtn);
        make.right.equalTo(self.footerView);
        make.height.mas_equalTo(UI_LOGIN_PROTOCOLBTN_HEIGHT);
    }];
}

- (void)makeMoreContraints{
    [self.moreView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.footerView.mas_bottom).offset(UI_LOGIN_MOREVIEW_TOP);
        make.left.right.equalTo(self.coreView);
        make.bottom.equalTo(self).offset(-UI_LOGIN_MOREVIEW_BOTTOM);
    }];
    
    [self.registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(UI_LOGIN_REGISTBTN_HEIGHT);
        make.bottom.equalTo(self.moreView);
        make.centerX.equalTo(self.moreView);
    }];
    
    [self.lineLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.moreView);
        make.centerX.equalTo(self.moreView);
    }];
    
    [self.leftSep mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.moreView);
        make.centerY.equalTo(self.lineLbl);
        make.width.mas_equalTo (UI_LOGIN_LEFTSEP_AND_RIGHTSEP_WIDTH);
        make.height.mas_equalTo(ME_CONSTRAINTS_LINE_WIDTH_COMMON_1);
    }];
    
    [self.rightSep mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.moreView);
        make.centerY.equalTo(self.lineLbl);
        make.width.mas_equalTo(UI_LOGIN_LEFTSEP_AND_RIGHTSEP_WIDTH);
        make.height.mas_equalTo(ME_CONSTRAINTS_LINE_WIDTH_COMMON_1);
    }];
    
    [self.quickLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineLbl.mas_bottom).offset(UI_LOGIN_QUICKLOGIN_TOP);
        make.left.right.equalTo(self.moreView);
        make.height.mas_equalTo(UI_LOGIN_QUICKLOGIN_HEIGHT);
    }];
}


#pragma mark -
#pragma mark api 

- (void)initLoginBtn{
    
    self.mainEventBtn.backgroundColor = [UIColor colorWithString:@"3695ff" andAlp:0.4];
    [self.mainEventBtn setTitleColor:[UIColor colorWithString:@"ffffff" andAlp:0.4] forState:UIControlStateNormal];
    self.mainEventBtn.userInteractionEnabled = NO;
    
}

- (void)activeLoginBtn{
    
    self.mainEventBtn.backgroundColor = [UIColor colorWithString:@"3695ff" andAlp:1.0];
    [self.mainEventBtn setTitleColor:[UIColor colorWithString:@"ffffff" andAlp:1.0] forState:UIControlStateNormal];
    self.mainEventBtn.userInteractionEnabled = YES;
    
}


- (void)hiddenExtraViewWith:(NSString *)mainTitle{
    self.moreView.hidden = YES;
    self.forgetPwdBtn.hidden = YES;
    [self.mainEventBtn setTitle:mainTitle forState:UIControlStateNormal];
}

#pragma  mark - even response

- (void)protocolClick:(UIButton *)sender{
    //to do
    self.protocolBtn.selected = !self.protocolBtn.selected;
    
}

- (void)rightProtocolClick:(UIButton *)sender {
    if (self.dismissCurrent) {
        self.dismissCurrent();
    }
}

- (void)forgetPwd:(UIButton *)sender{
    if (self.commonJump) {
        self.commonJump(sender.tag);
    }
}

- (void)registUser:(UIButton *)sender{
    if (self.commonJump) {
        self.commonJump(sender.tag);
    }
}

- (void)quickLogin:(UIButton *)sender{
    if (self.commonJump) {
        self.commonJump(sender.tag);
    }
}



@end
